﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObjects.BO_GetObjects;
using System.Data;

public partial class Pages_Survey : System.Web.UI.Page {
    private List<RadioButton> radios;
    private List<TextBox> textboxs;
    private List<CheckBox> checkboxs;
    private List<DropDownList> comboboxs;

    protected void Page_Load(object sender, EventArgs e) {
        LoadData();
    }

    protected void Page_Init(object sender, EventArgs e) {
    }

    protected void LoadData() {
        try {
            //DataTable objDT = BO_portal_survey.Display(1, 1);

            //List<Answer> listAnswers = new List<Answer>();
            //Answer answer = null;
            //foreach (DataRow dr in objDT.Rows) {
            //    answer = new Answer();
            //    answer.QUESTION_ID = Convert.ToInt32(dr["QUESTION_ID"]);
            //    answer.QUESTION_TEXT = dr["QUESTION_TEXT"].ToString();
            //    answer.ANSWER_ID = Convert.ToInt32(dr["ANSWER_ID"]);
            //    answer.ANSWER_TEXT = dr["ANSWER_TEXT"].ToString();
            //    answer.CONTROL_NAME = dr["CONTROL_NAME"].ToString();
            //    listAnswers.Add(answer);
            //}
            //pQuestionText.InnerHtml = answer.QUESTION_TEXT;

            //foreach (Answer a in listAnswers) {
            //    BuildSurvey(a.CONTROL_NAME, a.ANSWER_ID, a.ANSWER_TEXT, a.QUESTION_ID.ToString());
            //}

        } catch (Exception ex) {
            throw ex;
        }
    }

    protected void BuildSurvey(string control_name, int answer_id, string answer_text, string question_id) {
        try {
            switch (control_name) {
                case "Textbox":
                    CreateTextbox(answer_id);
                    break;
                case "Radio Buttons":
                    CreateRadioButtons(answer_id, answer_text, question_id);
                    break;
                case "Checkbox":
                    CreateCheckbox(answer_id, answer_text);
                    break;
                case "TextArea":
                    break;
                case "Combobox":
                    break;
                default:
                    break;
            }
        } catch (Exception ex) {
            throw ex;
        }
    }

    protected void CreateRadioButtons(int answer_id, string answer_text, string question_id) {
        try {
            radios = new List<RadioButton>();
            RadioButton rb = new RadioButton();
            rb.ID = "rb_" + answer_id.ToString();
            rb.Text = answer_text;
            rb.GroupName = "question_" + question_id;
            rb.CheckedChanged += new EventHandler(btnVote_Click);
            plHolder.Controls.Add(rb);
            radios.Add(rb);
        } catch (Exception ex) {
            throw ex;
        }
    }

    protected void CreateTextbox(int answer_id) {
        try {
            textboxs = new List<TextBox>();
            TextBox tb = new TextBox();
            tb.ID = "tb_" + answer_id.ToString();
            tb.TextChanged += new EventHandler(btnVote_Click);
            plHolder.Controls.Add(tb);
            textboxs.Add(tb);
        } catch (Exception ex) {
            throw ex;
        }
    }

    protected void CreateCheckbox(int answer_id, string answer_text) {
        try {
            checkboxs = new List<CheckBox>();
            CheckBox cb = new CheckBox();
            cb.ID = "cb_" + answer_id.ToString();
            cb.Text = answer_text;
            cb.CheckedChanged += new EventHandler(btnVote_Click);
            plHolder.Controls.Add(cb);
            checkboxs.Add(cb);
        } catch (Exception ex) {
            throw ex;
        }
    }

    protected void btnVote_Click(object sender, EventArgs e) {
        if (radios != null && radios.Count > 0) {
            foreach (RadioButton rb in radios) {
                if (rb.Checked)
                    Response.Write(rb.Text);
            }
        }
    }
}